﻿using System;
using System.Collections.Generic;
using System.Linq;
using YSC_Core.BLL.Domain.BackstageModels;
using YSC_Core.BLL.Domain.CommonModels;
using YSC_Core.BLL.Domain.Entity;
using YSC_Core.BLL.Domain.Utils;
using YSC_Core.BLL.Repository;
using YSC_OpenPlatform_API.Entity;
using YSC_Services.BLL.Services.Interfaces;

namespace YSC_Services.BLL.Services.Implementations
{
    public class BasisBackstageServices : IBasisBackstageServices
    {
        private IEfRepository _rep;

        public BasisBackstageServices(IEfRepository rep)
        {
            this._rep = rep;
        }

        /// <summary>
        /// 数据字典新增修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnValueModel AddUpdateDictionary(DictionaryModels model)
        {
            List<string> mgsList = new List<string>();
            mgsList.Add((string.IsNullOrEmpty(model.Type) ? "字典类型" : null));
            mgsList.Add((string.IsNullOrEmpty(model.Name) ? "字典名称" : null));
            foreach (var items in mgsList)
                if (!string.IsNullOrEmpty(items))
                    return new ReturnValueModel { Code = 401, Msg = String.Format("您的{0}没有填写!", items) };

            if (string.IsNullOrEmpty(model.Id))
            {
                Dictionary dictionary = new Dictionary()
                {
                    Id = Guid.NewGuid().ToString(),
                    Status = 0,
                    CreateTime = DateTime.Now,
                    CreateId = "4076e59b-c219-11e9-a7b7-48d53943449f",
                    Type = model.Type,
                    Name = model.Name,
                    Remarks = model.Remarks
                };

                _rep.Insert(dictionary);

                return new ReturnValueModel { Code = 200, Msg = "success", Result = "New Data OK!" };
            }

            var itemAll = _rep.Where<Dictionary>(p => p.Id == model.Id && p.Status == 0).ToList();
            if (itemAll.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到数据!" };

            var item = itemAll.FirstOrDefault();
            item.Type = !string.IsNullOrEmpty(model.Type) ? model.Type : item.Type;
            item.Name = !string.IsNullOrEmpty(model.Name) ? model.Name : item.Name;
            item.Remarks = !string.IsNullOrEmpty(model.Remarks) ? model.Remarks : item.Remarks;
            _rep.Update(item);
            return new ReturnValueModel { Code = 200, Msg = "success", Result = "Update Data OK!" };
        }

        /// <summary>
        /// 数据字典删除
        /// </summary>
        /// <param name="getDId">主键Id</param>
        /// <returns></returns>
        public ReturnValueModel DelectDictionary(string getDId)
        {
            if (string.IsNullOrEmpty(getDId))
                return new ReturnValueModel { Code = 401, Msg = String.Format("您的{0}没有填写!", "主键") };

            var itemAll = _rep.Where<Dictionary>(p => p.Id == getDId && p.Status == 0).ToList();
            if (itemAll.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到数据!" };

            var item = itemAll.FirstOrDefault();
            item.Status = -1;
            _rep.Update(item);
            return new ReturnValueModel { Code = 200, Msg = "success", Result = "Delect Data OK!" };
        }

        /// <summary>
        /// 数据字典详情
        /// </summary>
        /// <param name="getDId">主键Id</param>
        /// <returns></returns>
        public ReturnValueModel GetDictionary(string getDId)
        {
            if (string.IsNullOrEmpty(getDId))
                return new ReturnValueModel { Code = 400, Msg = String.Format("您的{0}没有填写!", "主键") };

            var itemAll = _rep.Where<Dictionary>(p => p.Id == getDId && p.Status == 0).ToList();
            if (itemAll.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到数据!" };

            var item = itemAll.FirstOrDefault();

            return new ReturnValueModel { Code = 200, Msg = "success", Result = new { Data = item } };
        }

        /// <summary>
        /// 数据字典列表
        /// </summary>
        /// <param name="index">起始页</param>
        /// <param name="size">每页数量</param>
        /// <param name="sasearch">查询条件</param>
        /// <returns></returns>
        public PaginationModel GetDictionaryAll(int index, int size, string sasearch)
        {
            var dictionaryAll = _rep.Where<Dictionary>(p => p.Status == 0)
                         .OrderByDescending(p => p.Type)
                         .WhereIf(p => p.Type.Contains(sasearch) || p.Type.Contains(sasearch), string.IsNullOrEmpty(sasearch) == false);
            var total = dictionaryAll.ToList().Count();

            var models = dictionaryAll
                         .ToPaginationList(index, size)
                         .ToList();

            return new PaginationModel { Code = 200, Msg = "success", Total = total, Result = new { Data = models } };
        }

        /// <summary>
        /// 数据字典类型列表
        /// </summary>
        /// <returns></returns>
        public PaginationModel GetDictionaryTypeAll()
        {
            var models = _rep.Where<Dictionary>(p => p.Status == 0).DistinctBy(p => p.Type).ToList();

            return new PaginationModel { Code = 200, Msg = "success", Total = models.Count(), Result = new { Data = models } };
        }

        /// <summary>
        /// 地区列表
        /// </summary>
        /// <param name="level">等级:1省份2市3区</param>
        /// <param name="code">上级编码</param>
        /// <param name="name">城市名称</param>
        /// <returns></returns>
        public PaginationModel GetAreaAll(int level, string code, string name)
        {

            if (!string.IsNullOrEmpty(name))
            {
                var sqlM = "WITH CTE AS ( SELECT YUnCode,PUnCode FROM dbo.Area_copy WHERE Areaname={0} UNION ALL SELECT a.YUnCode,a.PUnCode FROM dbo.Area_copy a INNER JOIN CTE b ON b.YUnCode=a.PUnCode) SELECT a.*  FROM dbo.Area_copy a INNER JOIN CTE t ON a.YUnCode=t.YUnCode";
                var areaAllM = _rep.SqlQuery<Area_copy>(sqlM, name).OrderBy(p => p.YunCode).Select(p => new
                {
                    p.YunCode,
                    p.AreaName,
                    p.PyCode,
                    p.PunCode,
                    p.Level
                }).ToList();
                return new PaginationModel { Code = 200, Msg = "success", Result = new { Data = areaAllM } };
            }

            var areaList = _rep.Where<Area_copy>(p => level == 0 ? p.Level != 3 : p.Level == level).OrderBy(p => p.YunCode).Select(p => new
            {
                p.YunCode,
                p.AreaName,
                p.PyCode,
                p.PunCode,
                p.Level
            })
            .WhereIf(p => p.PunCode == code, string.IsNullOrEmpty(code) == false)
            //.OrderByDescending(p => p.AreaName)
            .ToList();

            return new PaginationModel { Code = 200, Msg = "success", Result = new { Data = areaList } };
        }

        /// <summary>
        /// 市场单位列表
        /// </summary>
        /// <param name="index">起始页</param>
        /// <param name="size">每页数量</param>
        /// <param name="sasearch">查询条件</param>
        /// <returns></returns>
        public PaginationModel GetMarketUnitAll(int index, int size, string sasearch)
        {
            var marketUnitAll = from a in _rep.Where<MarketUnit>(p => p.Status == 0)
                                join r in _rep.Where<Area>(p => p.Status == 0) on a.AreaUnCode equals r.YunCode
                                select new
                                {
                                    a.Id,
                                    a.CreateId,
                                    a.CreateTime,
                                    a.Status,
                                    a.Remarks,
                                    a.MarketName,
                                    a.Address,
                                    a.UnCode,
                                    a.AreaUnCode,
                                    r.AreaName,
                                    a.ContactsName,
                                    a.ContactsPhone,
                                    a.Longitude,
                                    a.Latitude
                                };

            var marketUnitList = marketUnitAll.OrderByDescending(p => p.CreateTime)
                         .WhereIf(p => p.MarketName.Contains(sasearch) || p.ContactsName.Contains(sasearch) || p.ContactsPhone.Contains(sasearch) || p.Address.Contains(sasearch), string.IsNullOrEmpty(sasearch) == false);

            var total = marketUnitList.ToList().Count();

            var models = marketUnitList
             .ToPaginationList(index, size)
                     .ToList();

            return new PaginationModel { Code = 200, Msg = "success", Total = total, Result = new { Data = models } };
        }

        /// <summary>
        ///  市场单位详情
        /// </summary>
        /// <param name="GetMarketId">主键Id</param>
        /// <returns></returns>
        public ReturnValueModel GetMarketUnit(string GetMarketId)
        {
            if (string.IsNullOrEmpty(GetMarketId))
                return new ReturnValueModel { Code = 400, Msg = String.Format("您的{0}没有填写!", "主键") };

            var itemAll = _rep.Where<MarketUnit>(p => p.Id == GetMarketId && p.Status == 0).ToList();
            if (itemAll.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到数据!" };

            var item = itemAll.FirstOrDefault();

            return new ReturnValueModel { Code = 200, Msg = "success", Result = new { Data = item } };
        }

        /// <summary>
        ///  市场单位新增修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnValueModel AddUpdateMarketUnit(MarketUnitModels model)
        {
            List<string> mgsList = new List<string>();
            mgsList.Add((string.IsNullOrEmpty(model.MarketName) ? "市场名称" : null));
            mgsList.Add((string.IsNullOrEmpty(model.Address) ? "地址" : null));
            mgsList.Add((string.IsNullOrEmpty(model.AreaUnCode) ? "地区编码" : null));
            foreach (var item in mgsList)
                if (!string.IsNullOrEmpty(item))
                    return new ReturnValueModel { Code = 400, Msg = String.Format("您的{0}没有填写!", item) };

            if (string.IsNullOrEmpty(model.Id))
            {
                MarketUnit marketUnit = new MarketUnit()
                {
                    Id = Guid.NewGuid().ToString(),
                    Status = 0,
                    CreateTime = DateTime.Now,
                    CreateId = "4076e59b-c219-11e9-a7b7-48d53943449f",
                    MarketName = model.MarketName,
                    Address = model.Address,
                    AreaUnCode = model.AreaUnCode,
                    UnCode = UtilsHelp.getRandNum(6),
                    Longitude = model.Longitude,
                    Latitude = model.Latitude,
                    ContactsName = model.ContactsName,
                    ContactsPhone = model.ContactsPhone,
                    Remarks = model.Remarks
                };
                _rep.Insert(marketUnit);
                return new ReturnValueModel { Code = 200, Msg = "success", Result = "New Data OK!" };
            }

            var itemAll = _rep.Where<MarketUnit>(p => p.Id == model.Id && p.Status == 0).ToList();
            if (itemAll.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到数据!" };

            var items = itemAll.FirstOrDefault();
            items.MarketName = !string.IsNullOrEmpty(model.MarketName) ? model.MarketName : items.MarketName;
            items.Address = !string.IsNullOrEmpty(model.Address) ? model.Address : items.Address;
            items.AreaUnCode = !string.IsNullOrEmpty(model.AreaUnCode) ? model.AreaUnCode : items.AreaUnCode;
            items.UnCode = !string.IsNullOrEmpty(model.UnCode) ? model.UnCode : items.UnCode;
            items.Longitude = !string.IsNullOrEmpty(model.Longitude) ? model.Longitude : items.Longitude;
            items.Latitude = !string.IsNullOrEmpty(model.Latitude) ? model.Latitude : items.Latitude;
            items.ContactsName = !string.IsNullOrEmpty(model.ContactsName) ? model.ContactsName : items.ContactsName;
            items.ContactsPhone = !string.IsNullOrEmpty(model.ContactsPhone) ? model.ContactsPhone : items.ContactsPhone;
            items.Remarks = !string.IsNullOrEmpty(model.Remarks) ? model.Remarks : items.Remarks;
            _rep.Update(items);
            return new ReturnValueModel { Code = 200, Msg = "success", Result = "Update Data OK!" };

        }

        /// <summary>
        ///  市场单位删除
        /// </summary>
        /// <param name="GetMarketId">主键Id</param>
        /// <returns></returns>
        public ReturnValueModel DelectMarketUnit(string GetMarketId)
        {
            if (string.IsNullOrEmpty(GetMarketId))
                return new ReturnValueModel { Code = 400, Msg = String.Format("您的{0}没有填写!", "主键") };

            var itemAll = _rep.Where<MarketUnit>(p => p.Id == GetMarketId && p.Status == 0).ToList();
            if (itemAll.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到数据!" };

            var item = itemAll.FirstOrDefault();
            item.Status = -1;
            _rep.Update(item);
            return new ReturnValueModel { Code = 200, Msg = "success", Result = "Delect Data OK!" };
        }

        /// <summary>
        /// 鱼种列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="status"></param>
        /// <param name="sasearch"></param>
        /// <returns></returns>
        public PaginationModel GetFishSpeciesAll(int index, int size, int status, string sasearch)
        {
            var fishAll = from a in _rep.Where<FishSpecies>(p => status == 0 ? p.Status == 0 : p.Status == -1)
                          join d in _rep.Where<Dictionary>(p => p.Status == 0) on a.Type equals d.Id
                          select new
                          {
                              a.Id,
                              a.CreateId,
                              a.CreateTime,
                              a.Status,
                              a.Remarks,
                              a.Type,
                              TypeName = d.Name,
                              a.FishName,
                              a.Description,
                              a.FishCode,
                              a.SuName,
                              a.EnglishName,
                              a.Introduction,
                              a.YingYang,
                              a.PengRen,
                              a.XuanGou,
                              a.BaoXian,
                              a.TiShi,
                              Url = "123"
                          };



            var fishList = fishAll.OrderByDescending(p => p.CreateTime)
                         .WhereIf(p => p.TypeName.Contains(sasearch) || p.FishName.Contains(sasearch) || p.Description.Contains(sasearch) || p.FishCode.Contains(sasearch), string.IsNullOrEmpty(sasearch) == false);

            var total = fishList.ToList().Count();

            var models = fishList
             .ToPaginationList(index, size)
                     .ToList();

            return new PaginationModel { Code = 200, Msg = "success", Total = total, Result = new { Data = models } };
        }

        /// <summary>
        /// 鱼种详情
        /// </summary>
        /// <param name="fishId"></param>
        /// <returns></returns>
        public ReturnValueModel GetFishSpecies(string fishId)
        {
            if (string.IsNullOrEmpty(fishId))
                return new ReturnValueModel { Code = 400, Msg = String.Format("您的{0}没有填写!", "主键") };

            var itemAll = _rep.Where<FishSpecies>(p => p.Id == fishId).ToList();
            if (itemAll.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到数据!" };

            var item = itemAll.FirstOrDefault();

            return new ReturnValueModel { Code = 200, Msg = "success", Result = new { Data = item } };
        }

        /// <summary>
        /// 鱼种新增修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnValueModel AddUpdateFishSpecies(FishSpeciesModel model)
        {
            List<string> mgsList = new List<string>();
            mgsList.Add((string.IsNullOrEmpty(model.Type) ? "鱼种类型" : null));
            mgsList.Add((string.IsNullOrEmpty(model.FishName) ? "鱼种名称" : null));
            mgsList.Add((string.IsNullOrEmpty(model.FishCode) ? "鱼种编码" : null));
            mgsList.Add((string.IsNullOrEmpty(model.Description) ? "鱼种规格" : null));
            foreach (var item in mgsList)
                if (!string.IsNullOrEmpty(item))
                    return new ReturnValueModel { Code = 400, Msg = String.Format("您的{0}没有填写!", item) };

            if (string.IsNullOrEmpty(model.Id))
            {
                if (_rep.Where<FishSpecies>(p => p.FishCode == model.FishCode).ToList().Count() > 0)
                    return new ReturnValueModel { Code = 400, Msg = String.Format("鱼种编码已存在!") };
                FishSpecies fishSpecies = new FishSpecies()
                {
                    Id = Guid.NewGuid().ToString(),
                    Status = 0,
                    CreateTime = DateTime.Now,
                    CreateId = "4076e59b-c219-11e9-a7b7-48d53943449f",
                    Type = model.Type,
                    FishName = model.FishName,
                    Description = model.Description,
                    FishCode = model.FishCode,
                    SuName = model.SuName,
                    EnglishName = model.EnglishName,
                    LatinName = model.LatinName,
                    Introduction = model.Introduction,
                    YingYang = model.YingYang,
                    PengRen = model.PengRen,
                    XuanGou = model.XuanGou,
                    BaoXian = model.BaoXian,
                    TiShi = model.TiShi,
                    Remarks = model.Remarks
                };
                _rep.Insert(fishSpecies);
                return new ReturnValueModel { Code = 200, Msg = "success", Result = "New Data OK!" };
            }

            var itemAll = _rep.Where<FishSpecies>(p => p.Id == model.Id && p.Status == 0).ToList();
            if (itemAll.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到数据!" };

            var items = itemAll.FirstOrDefault();
            items.Type = !string.IsNullOrEmpty(model.Type) ? model.Type : items.Type;
            items.FishName = !string.IsNullOrEmpty(model.FishName) ? model.FishName : items.FishName;
            items.Description = !string.IsNullOrEmpty(model.Description) ? model.Description : items.Description;
            items.FishCode = !string.IsNullOrEmpty(model.FishCode) ? model.FishCode : items.FishCode;
            items.SuName = !string.IsNullOrEmpty(model.SuName) ? model.SuName : items.SuName;
            items.EnglishName = !string.IsNullOrEmpty(model.EnglishName) ? model.EnglishName : items.EnglishName;
            items.LatinName = !string.IsNullOrEmpty(model.LatinName) ? model.LatinName : items.LatinName;
            items.Introduction = !string.IsNullOrEmpty(model.Introduction) ? model.Introduction : items.Introduction;
            items.YingYang = !string.IsNullOrEmpty(model.YingYang) ? model.YingYang : items.YingYang;
            items.PengRen = !string.IsNullOrEmpty(model.PengRen) ? model.PengRen : items.PengRen;
            items.XuanGou = !string.IsNullOrEmpty(model.XuanGou) ? model.XuanGou : items.XuanGou;
            items.BaoXian = !string.IsNullOrEmpty(model.BaoXian) ? model.BaoXian : items.BaoXian;
            items.TiShi = !string.IsNullOrEmpty(model.TiShi) ? model.TiShi : items.TiShi;
            items.Remarks = !string.IsNullOrEmpty(model.Remarks) ? model.Remarks : items.Remarks;
            _rep.Update(items);
            return new ReturnValueModel { Code = 200, Msg = "success", Result = "Update Data OK!" };
        }


        /// <summary>
        /// 鱼种基本信息统一更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnValueModel AddMediaFishSpecies(FishSpeciesModel model)
        {
            var itemAll = _rep.Where<FishSpecies>(p => p.Id == model.Id && p.Status == 0).ToList();
            if (itemAll.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到数据!" };

            var fishAll = _rep.Where<FishSpecies>(p => p.FishName == itemAll.FirstOrDefault().FishName).ToList();

            foreach (var items in fishAll)
            {
                items.SuName = !string.IsNullOrEmpty(model.SuName) ? model.SuName : items.SuName;
                items.EnglishName = !string.IsNullOrEmpty(model.EnglishName) ? model.EnglishName : items.EnglishName;
                items.LatinName = !string.IsNullOrEmpty(model.LatinName) ? model.LatinName : items.LatinName;
                items.Introduction = !string.IsNullOrEmpty(model.Introduction) ? model.Introduction : items.Introduction;
                items.YingYang = !string.IsNullOrEmpty(model.YingYang) ? model.YingYang : items.YingYang;
                items.PengRen = !string.IsNullOrEmpty(model.PengRen) ? model.PengRen : items.PengRen;
                items.XuanGou = !string.IsNullOrEmpty(model.XuanGou) ? model.XuanGou : items.XuanGou;
                items.BaoXian = !string.IsNullOrEmpty(model.BaoXian) ? model.BaoXian : items.BaoXian;
                items.TiShi = !string.IsNullOrEmpty(model.TiShi) ? model.TiShi : items.TiShi;
                items.Remarks = !string.IsNullOrEmpty(model.Remarks) ? model.Remarks : items.Remarks;
            }

            _rep.UpdateList(fishAll);
            return new ReturnValueModel { Code = 200, Msg = "success", Result = "Update DataList OK!" };
        }


        /// <summary>
        ///  鱼种删除
        /// </summary>
        /// <param name="fishId">主键Id</param>
        /// <returns></returns>
        public ReturnValueModel DelectFishSpecies(string fishId)
        {
            if (string.IsNullOrEmpty(fishId))
                return new ReturnValueModel { Code = 400, Msg = String.Format("您的{0}没有填写!", "主键") };

            var itemAll = _rep.Where<FishSpecies>(p => p.Id == fishId && p.Status == 0).ToList();
            if (itemAll.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到数据!" };

            var item = itemAll.FirstOrDefault();
            item.Status = -1;
            _rep.Update(item);
            return new ReturnValueModel { Code = 200, Msg = "success", Result = "Delect Data OK!" };
        }


        /// <summary>
        /// 产地单位列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="sasearch"></param>
        /// <returns></returns>
        public PaginationModel GetPlaceOfOriginUnitXAll(int index, int size, string sasearch)
        {
            var marketUnitAll = from a in _rep.Where<PlaceOfOriginUnit>(p => p.Status == 0)
                                join r in _rep.Where<Area>(p => p.Status == 0) on a.AreaUnCode equals r.YunCode
                                select new
                                {
                                    a.Id,
                                    a.CreateId,
                                    a.CreateTime,
                                    a.Status,
                                    a.Remarks,
                                    a.PlaceOriginName,
                                    a.Address,
                                    a.UnCode,
                                    a.AreaUnCode,
                                    r.AreaName,
                                    a.ContactsName,
                                    a.ContactsPhone,
                                    a.Longitude,
                                    a.Latitude
                                };



            var marketUnitList = marketUnitAll.OrderByDescending(p => p.CreateTime)
                         .WhereIf(p => p.PlaceOriginName.Contains(sasearch) || p.ContactsName.Contains(sasearch) || p.ContactsPhone.Contains(sasearch) || p.Address.Contains(sasearch), string.IsNullOrEmpty(sasearch) == false);

            var total = marketUnitList.ToList().Count();

            var models = marketUnitList
             .ToPaginationList(index, size)
                     .ToList();

            return new PaginationModel { Code = 200, Msg = "success", Total = total, Result = models };
        }


        /// <summary>
        /// 产地单位列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="sasearch"></param>
        /// <returns></returns>
        public PaginationModel GetPlaceOfOriginUnitAll(int index, int size, string sasearch)
        {
            var marketUnitAll = from a in _rep.Where<PlaceOfOriginUnit>(p => p.Status == 0)
                                join r in _rep.Where<Area>(p => p.Status == 0) on a.AreaUnCode equals r.YunCode
                                select new
                                {
                                    a.Id,
                                    a.CreateId,
                                    a.CreateTime,
                                    a.Status,
                                    a.Remarks,
                                    a.PlaceOriginName,
                                    a.Address,
                                    a.UnCode,
                                    a.AreaUnCode,
                                    r.AreaName,
                                    a.ContactsName,
                                    a.ContactsPhone,
                                    a.Longitude,
                                    a.Latitude
                                };



            var marketUnitList = marketUnitAll.OrderByDescending(p => p.CreateTime)
                         .WhereIf(p => p.PlaceOriginName.Contains(sasearch) || p.ContactsName.Contains(sasearch) || p.ContactsPhone.Contains(sasearch) || p.Address.Contains(sasearch), string.IsNullOrEmpty(sasearch) == false);

            var total = marketUnitList.ToList().Count();

            var models = marketUnitList
             .ToPaginationList(index, size)
                     .ToList();

            return new PaginationModel { Code = 200, Msg = "success", Total = total, Result = models };
        }

        /// <summary>
        /// 产地单位详情
        /// </summary>
        /// <param name="placeOfO"></param>
        /// <returns></returns>
        public ReturnValueModel GetPlaceOfOriginUnit(string placeOfOId)
        {
            if (string.IsNullOrEmpty(placeOfOId))
                return new ReturnValueModel { Code = 400, Msg = String.Format("您的{0}没有填写!", "主键") };

            var itemAll = _rep.Where<PlaceOfOriginUnit>(p => p.Id == placeOfOId && p.Status == 0).ToList();
            if (itemAll.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到数据!" };

            var item = itemAll.FirstOrDefault();

            return new ReturnValueModel { Code = 200, Msg = "success", Result = new { Data = item } };
        }

        /// <summary>
        /// 新增修改产地单位
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnValueModel AddUpdatePlaceOfOriginUnit(PlaceOfOriginUnitModels model)
        {
            List<string> mgsList = new List<string>();
            mgsList.Add((string.IsNullOrEmpty(model.PlaceOriginName) ? "产地名称" : null));
            mgsList.Add((string.IsNullOrEmpty(model.Address) ? "地址" : null));
            mgsList.Add((string.IsNullOrEmpty(model.AreaUnCode) ? "地区编码" : null));
            foreach (var item in mgsList)
                if (!string.IsNullOrEmpty(item))
                    return new ReturnValueModel { Code = 400, Msg = String.Format("您的{0}没有填写!", item) };

            if (string.IsNullOrEmpty(model.Id))
            {
                PlaceOfOriginUnit placeOfOriginUnit = new PlaceOfOriginUnit()
                {
                    Id = Guid.NewGuid().ToString(),
                    Status = 0,
                    CreateTime = DateTime.Now,
                    CreateId = "4076e59b-c219-11e9-a7b7-48d53943449f",
                    PlaceOriginName = model.PlaceOriginName,
                    Address = model.Address,
                    AreaUnCode = model.AreaUnCode,
                    UnCode = UtilsHelp.getRandNum(6),
                    Longitude = model.Longitude,
                    Latitude = model.Latitude,
                    ContactsName = model.ContactsName,
                    ContactsPhone = model.ContactsPhone,
                    Remarks = model.Remarks
                };
                _rep.Insert(placeOfOriginUnit);
                return new ReturnValueModel { Code = 200, Msg = "success", Result = "New Data OK!" };
            }

            var itemAll = _rep.Where<PlaceOfOriginUnit>(p => p.Id == model.Id && p.Status == 0).ToList();
            if (itemAll.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到数据!" };

            var items = itemAll.FirstOrDefault();
            items.PlaceOriginName = !string.IsNullOrEmpty(model.PlaceOriginName) ? model.PlaceOriginName : items.PlaceOriginName;
            items.Address = !string.IsNullOrEmpty(model.Address) ? model.Address : items.Address;
            items.AreaUnCode = !string.IsNullOrEmpty(model.AreaUnCode) ? model.AreaUnCode : items.AreaUnCode;
            items.UnCode = !string.IsNullOrEmpty(model.UnCode) ? model.UnCode : items.UnCode;
            items.Longitude = !string.IsNullOrEmpty(model.Longitude) ? model.Longitude : items.Longitude;
            items.Latitude = !string.IsNullOrEmpty(model.Latitude) ? model.Latitude : items.Latitude;
            items.ContactsName = !string.IsNullOrEmpty(model.ContactsName) ? model.ContactsName : items.ContactsName;
            items.ContactsPhone = !string.IsNullOrEmpty(model.ContactsPhone) ? model.ContactsPhone : items.ContactsPhone;
            items.Remarks = !string.IsNullOrEmpty(model.Remarks) ? model.Remarks : items.Remarks;
            _rep.Update(items);
            return new ReturnValueModel { Code = 200, Msg = "success", Result = "Update Data OK!" };
        }

        /// <summary>
        /// 产地单位删除
        /// </summary>
        /// <param name="GetMarketId"></param>
        /// <returns></returns>
        public ReturnValueModel DelectPlaceOfOriginUnit(string placeOfOId)
        {
            if (string.IsNullOrEmpty(placeOfOId))
                return new ReturnValueModel { Code = 400, Msg = String.Format("您的{0}没有填写!", "主键") };

            var itemAll = _rep.Where<PlaceOfOriginUnit>(p => p.Id == placeOfOId && p.Status == 0).ToList();
            if (itemAll.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到数据!" };

            var item = itemAll.FirstOrDefault();
            item.Status = -1;
            _rep.Update(item);
            return new ReturnValueModel { Code = 200, Msg = "success", Result = "Delect Data OK!" };
        }

        /// <summary>
        /// 添加修改文章信息
        /// </summary>
        /// <param name="newsArticle"></param>
        /// <returns></returns>
        public ReturnValueModel AddUpdateNewsArticle(NewsArticle entity)
        {

            if (entity.Id == "")
            {
                entity.Id = Guid.NewGuid().ToString();
                entity.CreateId = "4076e59b-c219-11e9-a7b7-48d53943449f";
                entity.CreateTime = DateTime.Now;
                _rep.Insert(entity);

                return new ReturnValueModel { Code = 200, Msg = "success", Result = "New Data OK!" };
            }
            var item = _rep.Where<NewsArticle>(p => p.Id == entity.Id && p.Status != -1).FirstOrDefault();
            if (item == null)
                return new ReturnValueModel { Code = 404, Msg = "查找不到数据!" };
            item.Type = !string.IsNullOrEmpty(entity.Type) ? entity.Type : item.Type;
            item.Details = !string.IsNullOrEmpty(entity.Details) ? entity.Details : item.Details;
            item.Title = !string.IsNullOrEmpty(entity.Title) ? entity.Title : item.Title;
            item.Remarks = !string.IsNullOrEmpty(entity.Remarks) ? entity.Remarks : item.Remarks;
            _rep.Update(item);
            return new ReturnValueModel { Code = 200, Msg = "success", Result = "New Data OK!" };

        }
        /// <summary>
        /// 文章详情
        /// </summary>
        /// <param name="getDId"></param>
        /// <returns></returns>
        public ReturnValueModel GetNewsArticle(string getDId)
        {
            if (string.IsNullOrEmpty(getDId))
                return new ReturnValueModel { Code = 400, Msg = String.Format("您的{0}没有填写!", "主键") };

            var item = _rep.Where<NewsArticle>(p => p.Id == getDId && p.Status != -1).FirstOrDefault();
            if (item == null)
                return new ReturnValueModel { Code = 404, Msg = "查找不到数据!" };

            return new ReturnValueModel { Code = 200, Msg = "success", Result = new { Data = item } };
        }

        /// <summary>
        /// 文章列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="sasearch"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public PaginationModel GetNewsArticleAll(int index, int size, string sasearch, string type)
        {

            var newsArticleAll = _rep.Where<NewsArticle>(p => p.Status != -1)
                    .OrderByDescending(p => p.Id)
                    .WhereIf(p => p.Title.Contains(sasearch), string.IsNullOrEmpty(sasearch) == false)
              .WhereIf(p => p.Type.Equals(type), string.IsNullOrEmpty(type) == false).ToList().Select(s => new
              {
                  s.Id,
                  s.Title,
                  s.Type,
                  s.Author,
                  ReleaseTime = s.ReleaseTime == null ? "" : Convert.ToDateTime(s.ReleaseTime).ToString("yyyy-MM-dd"),
                  CreateTime = s.CreateTime.ToString("yyyy-MM-dd "),
                  IsImages = _rep.Where<MediaImage>(p => p.BllId == s.Id).ToList().Count() == 0 ? false : true,
                  s.Status
              });
            //      var list = _rep.SqlQuery<NewsArticleModel>("select Id, ReleaseTime, Type, Author, Title, (select count(*) from MediaImage where bllid = newsarticle.Id)count from newsarticle ");

            var total = newsArticleAll.ToList().Count();

            var models = newsArticleAll
                         .ToPaginationList(index, size)
                         .ToList();

            return new PaginationModel { Code = 200, Msg = "success", Total = total, Result = new { Data = models } };
        }
        /// <summary>
        /// 根据Id修改状态
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public ReturnValueModel GetUpdateStatusById(string Id, string type)
        {

            if (string.IsNullOrEmpty(Id) && string.IsNullOrEmpty(type))
                return new ReturnValueModel { Code = 400, Msg = "参数错误" };

            var item = _rep.Where<NewsArticle>(p => p.Id == Id && p.Status != -1).FirstOrDefault();
            if (item == null)
                return new ReturnValueModel { Code = 404, Msg = "查找不到数据!" };


            if (type == "edit")
            {
                item.Status = item.Status == 1 ? 0 : 1;
                _rep.Update(item);
                return new ReturnValueModel { Code = 200, Msg = "success", Result = "New Data OK!" };
            }
            else
            {
                item.Status = -1;
                _rep.Update(item);
                return new ReturnValueModel { Code = 200, Msg = "success", Result = "New Data OK!" };
            }


        }

        /// <summary>
        /// 媒体信息新增
        /// </summary>
        /// <param name="billId"></param>
        /// <param name="billtype"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public ReturnValueModel AddUpdateMediaImageUpdate(MediaImageModel model)
        {
            if (string.IsNullOrEmpty(model.BllId)
                 || string.IsNullOrEmpty(model.Type)
                 || string.IsNullOrEmpty(model.FileUrl)
                 || string.IsNullOrEmpty(model.BllType))
                return new ReturnValueModel { Code = 400, Msg = "提交信息错误!" };

            //鱼种删除
            if (model.TypeInt == 1)
            {
                var mediaImagesAll = _rep.Where<MediaImage>(p => p.Type == model.Type).ToList();
                if (mediaImagesAll.Count() > 0)
                    _rep.RemoveRange(mediaImagesAll);
            }
            var mediaImageAll = _rep.Where<MediaImage>(p => p.BllId == model.BllId).ToList();
            if (mediaImageAll.Count() > 0)
                _rep.RemoveRange(mediaImageAll);

            MediaImage mediaImage = new MediaImage()
            {
                Id = Guid.NewGuid().ToString(),
                Status = 0,
                CreateTime = DateTime.Now,
                CreateId = "4076e59b-c219-11e9-a7b7-48d53943449f",
                BllId = model.BllId,
                BllType = model.BllType,
                FileUrl = model.FileUrl,
                Author = "管理员",
                Type = model.Type,
            };

            _rep.Insert(mediaImage);
            return new ReturnValueModel { Code = 200, Msg = "success", Result = "New Data OK!" };
        }


        /// <summary>
        /// 鱼种报表
        /// </summary>
        /// <param name="fishName"></param>
        /// <param name="province"></param>
        /// <param name="city"></param>
        /// <param name="unitType"></param>
        /// <param name="dateYear"></param>
        /// <returns></returns>
        public ReturnValueModel FishSpeciesMonthReport(string dateYear, string fishName, string province, string city, int unitType)
        {
            var sqlM = string.Empty;
            var areaAllM = new List<Area>();

            if (string.IsNullOrEmpty(city) && string.IsNullOrEmpty(province))
            {
                sqlM = "WITH CTE AS ( SELECT YUnCode,PUnCode FROM dbo.Area WHERE PUnCode=0 UNION ALL SELECT a.YUnCode,a.PUnCode FROM dbo.Area a INNER JOIN CTE b ON b.YUnCode=a.PUnCode) SELECT a.*  FROM dbo.Area a INNER JOIN CTE t ON a.YUnCode=t.YUnCode";
                areaAllM = _rep.SqlQuery<Area>(sqlM).ToList();
            }
            else if (!string.IsNullOrEmpty(city))
            {
                sqlM = "WITH CTE AS ( SELECT YUnCode,PUnCode FROM dbo.Area WHERE Areaname={0} UNION ALL SELECT a.YUnCode,a.PUnCode FROM dbo.Area a INNER JOIN CTE b ON b.YUnCode=a.PUnCode) SELECT a.*  FROM dbo.Area a INNER JOIN CTE t ON a.YUnCode=t.YUnCode";
                areaAllM = _rep.SqlQuery<Area>(sqlM, string.IsNullOrEmpty(city) ? "上海市" : city).Select(p => new Area { YunCode = p.YunCode }).ToList();
            }
            else
            {
                sqlM = "WITH CTE AS ( SELECT YUnCode,PUnCode FROM dbo.Area WHERE PUnCode={0} UNION ALL SELECT a.YUnCode,a.PUnCode FROM dbo.Area a INNER JOIN CTE b ON b.YUnCode=a.PUnCode) SELECT a.*  FROM dbo.Area a INNER JOIN CTE t ON a.YUnCode=t.YUnCode";
                areaAllM = _rep.SqlQuery<Area>(sqlM, province).Select(p => new Area { YunCode = p.YunCode }).ToList();
            }

            List<string> arrtM = new List<string>();
            foreach (var item in areaAllM)
                arrtM.Add(item.YunCode);
            string[] arrt2M = arrtM.ToArray();


            if (unitType == 0)
            {
                var MarketPriceAll = from a in _rep.Where<MarketPrice>(p => p.Status == 0)
                                     where a.CreateTime.Year.ToString() == dateYear
                                     join m in _rep.Where<MarketUnit>(p => p.Status == 0) on a.MarketId equals m.Id
                                     join r in _rep.Where<Area>(p => p.Status == 0) on m.AreaUnCode equals r.YunCode
                                     where arrt2M.Contains(r.YunCode)
                                     // join y in _rep.Where<YscUser>(p => p.Status == 0) on a.UserId equals y.Id
                                     join f in _rep.Where<FishSpecies>(p => p.Status == 0) on a.FishId equals f.FishCode
                                     join d in _rep.Where<Dictionary>(p => p.Status == 0) on f.Type equals d.Id
                                     select new
                                     {
                                         a.Id,
                                         f.FishName,
                                         fishType = d.Name,
                                         // y.UserName,
                                         a.UserId,
                                         m.Address,
                                         a.AveragePrice,
                                         a.CreateTime
                                     };

                var modelM = MarketPriceAll.OrderByDescending(p => p.CreateTime)
                       .WhereIf(p => p.FishName == fishName, string.IsNullOrEmpty(fishName) == false)
                        .GroupBy(m => new { m.CreateTime.Month })
                         .Select(a => new
                         {
                             a.Key.Month,
                             AveragePrice = Math.Round(Convert.ToDecimal(a.Average(p => p.AveragePrice)), 2)
                         }).ToList();

                return new ReturnValueModel { Code = 200, Msg = "success", Result = modelM };
            }

            var PlaceOfOriginAll = from a in _rep.Where<PlaceOfOriginPrice>(p => p.Status == 0)
                                   where a.CreateTime.Year.ToString() == dateYear
                                   join m in _rep.Where<PlaceOfOriginUnit>(p => p.Status == 0) on a.PlaceOfOriginId equals m.Id
                                   join r in _rep.Where<Area>(p => p.Status == 0) on m.AreaUnCode equals r.YunCode
                                   where arrt2M.Contains(r.YunCode)
                                   // join y in _rep.Where<YscUser>(p => p.Status == 0) on a.UserId equals y.Id
                                   join f in _rep.Where<FishSpecies>(p => p.Status == 0) on a.FishId equals f.FishCode
                                   join d in _rep.Where<Dictionary>(p => p.Status == 0) on f.Type equals d.Id
                                   select new
                                   {
                                       a.Id,
                                       f.FishName,
                                       fishType = d.Name,
                                       // y.UserName,
                                       a.UserId,
                                       m.Address,
                                       a.AveragePrice,
                                       a.CreateTime
                                   };

            var listtest = PlaceOfOriginAll.ToList();

            var model = PlaceOfOriginAll.OrderByDescending(p => p.CreateTime)
                   .WhereIf(p => p.FishName == fishName, string.IsNullOrEmpty(fishName) == false)
                   .GroupBy(m => new { m.CreateTime.Month })
                         .Select(a => new
                         {
                             a.Key.Month,
                             AveragePrice = Math.Round(Convert.ToDecimal(a.Average(p => p.AveragePrice)), 2)
                         }).ToList();

            return new ReturnValueModel { Code = 200, Msg = "success", Result = model };

        }


        ReturnValueModel IBasisBackstageServices.FishSpeciesYearReport(string fishType, string province, string city, string dateYear, int unitType)
        {
            var sqlM = string.Empty;
            var areaAllM = new List<Area>();
            if (string.IsNullOrEmpty(dateYear))
                dateYear = DateTime.Now.Year.ToString();

            if (string.IsNullOrEmpty(city) && string.IsNullOrEmpty(province))
            {
                sqlM = "WITH CTE AS ( SELECT YUnCode,PUnCode FROM dbo.Area WHERE PUnCode=0 UNION ALL SELECT a.YUnCode,a.PUnCode FROM dbo.Area a INNER JOIN CTE b ON b.YUnCode=a.PUnCode) SELECT a.*  FROM dbo.Area a INNER JOIN CTE t ON a.YUnCode=t.YUnCode";
                areaAllM = _rep.SqlQuery<Area>(sqlM).ToList();
            }
            else if (!string.IsNullOrEmpty(city))
            {
                sqlM = "WITH CTE AS ( SELECT YUnCode,PUnCode FROM dbo.Area WHERE Areaname={0} UNION ALL SELECT a.YUnCode,a.PUnCode FROM dbo.Area a INNER JOIN CTE b ON b.YUnCode=a.PUnCode) SELECT a.*  FROM dbo.Area a INNER JOIN CTE t ON a.YUnCode=t.YUnCode";
                areaAllM = _rep.SqlQuery<Area>(sqlM, string.IsNullOrEmpty(city) ? "上海市" : city).Select(p => new Area { YunCode = p.YunCode }).ToList();
            }
            else
            {
                sqlM = "WITH CTE AS ( SELECT YUnCode,PUnCode FROM dbo.Area WHERE PUnCode={0} UNION ALL SELECT a.YUnCode,a.PUnCode FROM dbo.Area a INNER JOIN CTE b ON b.YUnCode=a.PUnCode) SELECT a.*  FROM dbo.Area a INNER JOIN CTE t ON a.YUnCode=t.YUnCode";
                areaAllM = _rep.SqlQuery<Area>(sqlM, province).Select(p => new Area { YunCode = p.YunCode }).ToList();
            }

            List<string> arrtM = new List<string>();
            foreach (var item in areaAllM)
                arrtM.Add(item.YunCode);
            string[] arrt2M = arrtM.ToArray();


            if (unitType == 0)
            {
                var MarketPriceAll = from a in _rep.Where<MarketPrice>(p => p.Status == 0)
                                     where a.CreateTime.Year.ToString() == dateYear
                                     join m in _rep.Where<MarketUnit>(p => p.Status == 0) on a.MarketId equals m.Id
                                     join r in _rep.Where<Area>(p => p.Status == 0) on m.AreaUnCode equals r.YunCode
                                     where arrt2M.Contains(r.YunCode)
                                     // join y in _rep.Where<YscUser>(p => p.Status == 0) on a.UserId equals y.Id
                                     join f in _rep.Where<FishSpecies>(p => p.Status == 0) on a.FishId equals f.FishCode
                                     join d in _rep.Where<Dictionary>(p => p.Status == 0) on f.Type equals d.Id
                                     select new
                                     {
                                         a.Id,
                                         f.FishName,
                                         fishType = d.Name,
                                         // y.UserName,
                                         a.UserId,
                                         m.Address,
                                         a.AveragePrice,
                                         a.CreateTime
                                     };

                var modelM = MarketPriceAll.OrderByDescending(p => p.CreateTime)
                       .WhereIf(p => p.fishType == fishType, string.IsNullOrEmpty(fishType) == false)
                        .GroupBy(m => new { m.FishName })
                         .Select(a => new
                         {
                             a.Key.FishName,
                             AveragePrice = Math.Round(Convert.ToDecimal(a.Average(p => p.AveragePrice)), 2)
                         }).ToList();

                return new ReturnValueModel { Code = 200, Msg = "success", Result = modelM };
            }

            var PlaceOfOriginAll = from a in _rep.Where<PlaceOfOriginPrice>(p => p.Status == 0)
                                   where a.CreateTime.Year.ToString() == dateYear
                                   join m in _rep.Where<PlaceOfOriginUnit>(p => p.Status == 0) on a.PlaceOfOriginId equals m.Id
                                   join r in _rep.Where<Area>(p => p.Status == 0) on m.AreaUnCode equals r.YunCode
                                   where arrt2M.Contains(r.YunCode)
                                   // join y in _rep.Where<YscUser>(p => p.Status == 0) on a.UserId equals y.Id
                                   join f in _rep.Where<FishSpecies>(p => p.Status == 0) on a.FishId equals f.FishCode
                                   join d in _rep.Where<Dictionary>(p => p.Status == 0) on f.Type equals d.Id
                                   select new
                                   {
                                       a.Id,
                                       f.FishName,
                                       fishType = d.Name,
                                       // y.UserName,
                                       a.UserId,
                                       m.Address,
                                       a.AveragePrice,
                                       a.CreateTime
                                   };

            var model = PlaceOfOriginAll.OrderByDescending(p => p.CreateTime)
                   .WhereIf(p => p.fishType == fishType, string.IsNullOrEmpty(fishType) == false)
                    .GroupBy(m => new { m.FishName })
                     .Select(a => new
                     {
                         a.Key.FishName,
                         AveragePrice = Math.Round(Convert.ToDecimal(a.Average(p => p.AveragePrice)), 2)
                     }).ToList();

            return new ReturnValueModel { Code = 200, Msg = "success", Result = model };
        }

        public ReturnValueModel MarketReport(string province, string city)
        {
            var sqlM = string.Empty;
            var areaAllM = new List<Area>();

            if (string.IsNullOrEmpty(city) && string.IsNullOrEmpty(province))
            {
                sqlM = "WITH CTE AS ( SELECT YUnCode,PUnCode FROM dbo.Area WHERE PUnCode=0 UNION ALL SELECT a.YUnCode,a.PUnCode FROM dbo.Area a INNER JOIN CTE b ON b.YUnCode=a.PUnCode) SELECT a.*  FROM dbo.Area a INNER JOIN CTE t ON a.YUnCode=t.YUnCode";
                areaAllM = _rep.SqlQuery<Area>(sqlM).ToList();
            }
            else if (!string.IsNullOrEmpty(city))
            {
                sqlM = "WITH CTE AS ( SELECT YUnCode,PUnCode FROM dbo.Area WHERE Areaname={0} UNION ALL SELECT a.YUnCode,a.PUnCode FROM dbo.Area a INNER JOIN CTE b ON b.YUnCode=a.PUnCode) SELECT a.*  FROM dbo.Area a INNER JOIN CTE t ON a.YUnCode=t.YUnCode";
                areaAllM = _rep.SqlQuery<Area>(sqlM, string.IsNullOrEmpty(city) ? "上海市" : city).Select(p => new Area { YunCode = p.YunCode }).ToList();
            }
            else
            {
                sqlM = "WITH CTE AS ( SELECT YUnCode,PUnCode FROM dbo.Area WHERE PUnCode={0} UNION ALL SELECT a.YUnCode,a.PUnCode FROM dbo.Area a INNER JOIN CTE b ON b.YUnCode=a.PUnCode) SELECT a.*  FROM dbo.Area a INNER JOIN CTE t ON a.YUnCode=t.YUnCode";
                areaAllM = _rep.SqlQuery<Area>(sqlM, province).Select(p => new Area { YunCode = p.YunCode }).ToList();
            }

            List<string> arrtM = new List<string>();
            foreach (var item in areaAllM)
                arrtM.Add(item.YunCode);
            string[] arrt2M = arrtM.ToArray();

            var MarketUnitAll = from m in _rep.Where<MarketUnit>(p => p.Status == 0)
                                join r in _rep.Where<Area>(p => p.Status == 0) on m.AreaUnCode equals r.YunCode
                                where arrt2M.Contains(r.YunCode)
                                select new
                                {
                                    m.MarketName,
                                };


            var model = MarketUnitAll.OrderByDescending(p => p.MarketName)
                    .ToList();

            return new ReturnValueModel { Code = 200, Msg = "success", Result = model };
        }


        /// <summary>
        /// 市场鱼价_走势报表
        /// </summary>
        /// <param name="fishName">鱼种</param>
        /// <param name="province">省份</param>
        /// <param name="city">城市</param>
        /// <param name="type">报表类型:0一周，1三月2一年</param>
        /// <returns></returns>  
        public ReturnValueModel GetMarketPriceTrendReport(string fishName, string province, string city, int type)
        {
            var sqlM = string.Empty;
            var areaAllM = new List<Area>();
            //if (string.IsNullOrEmpty(dateYear))
            //    dateYear = DateTime.Now.Year.ToString();

            var fishIdM = _rep.FirstOrDefault<FishSpecies>(p => p.FishName == fishName).FishCode;

            if (string.IsNullOrEmpty(city) && string.IsNullOrEmpty(province))
            {
                sqlM = "WITH CTE AS ( SELECT YUnCode,PUnCode FROM dbo.Area WHERE PUnCode=0 UNION ALL SELECT a.YUnCode,a.PUnCode FROM dbo.Area a INNER JOIN CTE b ON b.YUnCode=a.PUnCode) SELECT a.*  FROM dbo.Area a INNER JOIN CTE t ON a.YUnCode=t.YUnCode";
                areaAllM = _rep.SqlQuery<Area>(sqlM).ToList();
            }
            else if (!string.IsNullOrEmpty(city))
            {
                sqlM = "WITH CTE AS ( SELECT YUnCode,PUnCode FROM dbo.Area WHERE Areaname={0} UNION ALL SELECT a.YUnCode,a.PUnCode FROM dbo.Area a INNER JOIN CTE b ON b.YUnCode=a.PUnCode) SELECT a.*  FROM dbo.Area a INNER JOIN CTE t ON a.YUnCode=t.YUnCode";
                areaAllM = _rep.SqlQuery<Area>(sqlM, string.IsNullOrEmpty(city) ? "上海市" : city).Select(p => new Area { YunCode = p.YunCode }).ToList();
            }
            else
            {
                sqlM = "WITH CTE AS ( SELECT YUnCode,PUnCode FROM dbo.Area WHERE PUnCode={0} UNION ALL SELECT a.YUnCode,a.PUnCode FROM dbo.Area a INNER JOIN CTE b ON b.YUnCode=a.PUnCode) SELECT a.*  FROM dbo.Area a INNER JOIN CTE t ON a.YUnCode=t.YUnCode";
                areaAllM = _rep.SqlQuery<Area>(sqlM, province).Select(p => new Area { YunCode = p.YunCode }).ToList();
            }

            List<string> arrtM = new List<string>();
            foreach (var item in areaAllM)
                arrtM.Add(item.YunCode);
            string[] arrt2M = arrtM.ToArray();


            if (type == 0)
            {
                var MarketPriceTrendReport = _rep.Where<MarketPrice>(p => p.FishId == fishIdM && p.CreateTime >= Convert.ToDateTime(DateTime.Now.AddDays(-7))).ToList().OrderByDescending(p => p.CreateTime)
                    .GroupBy(p => new
                    {
                        Day = UtilsHelp.Condition(Convert.ToDateTime(p.CreateTime), "Day")
                    })
                    .Select(p => new
                    {
                        p.Key.Day,
                        Price = Math.Round(Convert.ToDecimal(p.Average(a => a.AveragePrice)), 2)
                    }).ToList();

                var MarketPriceTrendAreaReportAll = from a in _rep.Where<MarketPrice>(p => p.FishId == fishIdM && p.CreateTime >= Convert.ToDateTime(DateTime.Now.AddDays(-7)))
                                                    join m in _rep.Where<MarketUnit>(p => true) on a.MarketId equals m.Id
                                                    join r in _rep.Where<Area>(p => true) on m.AreaUnCode equals r.YunCode
                                                    where arrt2M.Contains(r.YunCode)
                                                    select new
                                                    {
                                                        a.CreateTime,
                                                        a.AveragePrice
                                                    };
                var MarketPriceTrendAreaReport = MarketPriceTrendAreaReportAll.ToList()
                                                   .GroupBy(p => new
                                                   {
                                                       Day = UtilsHelp.Condition(Convert.ToDateTime(p.CreateTime), "Day")
                                                   })
                                                  .Select(p => new
                                                  {
                                                      p.Key.Day,
                                                      Price = Math.Round(Convert.ToDecimal(p.Average(a => a.AveragePrice)), 2)
                                                  }).ToList();


                return new ReturnValueModel { Code = 200, Msg = "success", Result = new { Date = new { National = MarketPriceTrendReport, Area = MarketPriceTrendReport } } };
            }

            if (type == 1)
            {
                var MarketPriceTrendReport = _rep.Where<MarketPrice>(p => p.FishId == fishIdM && p.CreateTime >= DateTime.Now.AddMonths(-3)).ToList().OrderByDescending(p => p.CreateTime)
                  .GroupBy(p => new
                  {
                      Month = UtilsHelp.Condition(Convert.ToDateTime(p.CreateTime), "Month")
                  })
                  .Select(p => new
                  {
                      Day = p.Key.Month,
                      Price = Math.Round(Convert.ToDecimal(p.Average(a => a.AveragePrice)), 2)
                  }).ToList();

                var MarketPriceTrendAreaReportAll = from a in _rep.Where<MarketPrice>(p => p.FishId == fishIdM && p.CreateTime >= Convert.ToDateTime(DateTime.Now.AddDays(-7)))
                                                    join m in _rep.Where<MarketUnit>(p => true) on a.MarketId equals m.Id
                                                    join r in _rep.Where<Area>(p => true) on m.AreaUnCode equals r.YunCode
                                                    where arrt2M.Contains(r.YunCode)
                                                    select new
                                                    {
                                                        a.CreateTime,
                                                        a.AveragePrice
                                                    };
                var MarketPriceTrendAreaReport = MarketPriceTrendAreaReportAll.ToList().OrderByDescending(p => p.CreateTime)
                                                   .GroupBy(p => new
                                                   {
                                                       Month = UtilsHelp.Condition(Convert.ToDateTime(p.CreateTime), "Month")
                                                   })
                                                  .Select(p => new
                                                  {
                                                      Day = p.Key.Month,
                                                      Price = Math.Round(Convert.ToDecimal(p.Average(a => a.AveragePrice)), 2)
                                                  }).ToList().OrderByDescending(p => p.Day);


                return new ReturnValueModel { Code = 200, Msg = "success", Result = new { Date = new { National = MarketPriceTrendReport, Area = MarketPriceTrendReport } } };
            }

            var MarketPriceTrendYReportM = _rep.Where<MarketPrice>(p => p.FishId == fishIdM && p.CreateTime >= DateTime.Now.AddMonths(-12)).ToList().OrderByDescending(p => p.CreateTime)
                  .GroupBy(p => new
                  {
                      Month = UtilsHelp.Condition(Convert.ToDateTime(p.CreateTime), "Month")
                  })
                  .Select(p => new
                  {
                      Day = p.Key.Month,
                      Price = Math.Round(Convert.ToDecimal(p.Average(a => a.AveragePrice)), 2)
                  }).ToList();

            var MarketPriceTrendAreaYReportAllM = from a in _rep.Where<MarketPrice>(p => p.FishId == fishIdM && p.CreateTime >= Convert.ToDateTime(DateTime.Now.AddDays(-7)))
                                                  join m in _rep.Where<MarketUnit>(p => true) on a.MarketId equals m.Id
                                                  join r in _rep.Where<Area>(p => true) on m.AreaUnCode equals r.YunCode
                                                  where arrt2M.Contains(r.YunCode)
                                                  select new
                                                  {
                                                      a.CreateTime,
                                                      a.AveragePrice
                                                  };
            var MarketPriceTrendAreaYReportM = MarketPriceTrendAreaYReportAllM.ToList()
                                               .GroupBy(p => new
                                               {
                                                   Month = UtilsHelp.Condition(Convert.ToDateTime(p.CreateTime), "Month")
                                               })
                                              .Select(p => new
                                              {
                                                  Day = p.Key.Month,
                                                  Price = Math.Round(Convert.ToDecimal(p.Average(a => a.AveragePrice)), 2)
                                              }).ToList().OrderByDescending(p => p.Day);


            return new ReturnValueModel { Code = 200, Msg = "success", Result = new { Date = new { National = MarketPriceTrendYReportM, Area = MarketPriceTrendYReportM } } };
        }

        /// <summary>
        /// 塘口鱼价_走势报表
        /// </summary>
        /// <param name="fishName">鱼种</param>
        /// <param name="province">省份</param>
        /// <param name="city">城市</param>
        /// <param name="type">报表类型:0一周，1三月2一年</param>
        /// <returns></returns>
        public ReturnValueModel GetPlaceOfOriginPriceTrendReport(string fishName, string province, string city, int type)
        {
            var sqlM = string.Empty;
            var areaAllM = new List<Area>();
            //if (string.IsNullOrEmpty(dateYear))
            //    dateYear = DateTime.Now.Year.ToString();

            var fishIdM = _rep.FirstOrDefault<FishSpecies>(p => p.FishName == fishName).FishCode;

            if (string.IsNullOrEmpty(city) && string.IsNullOrEmpty(province))
            {
                sqlM = "WITH CTE AS ( SELECT YUnCode,PUnCode FROM dbo.Area WHERE PUnCode=0 UNION ALL SELECT a.YUnCode,a.PUnCode FROM dbo.Area a INNER JOIN CTE b ON b.YUnCode=a.PUnCode) SELECT a.*  FROM dbo.Area a INNER JOIN CTE t ON a.YUnCode=t.YUnCode";
                areaAllM = _rep.SqlQuery<Area>(sqlM).ToList();
            }
            else if (!string.IsNullOrEmpty(city))
            {
                sqlM = "WITH CTE AS ( SELECT YUnCode,PUnCode FROM dbo.Area WHERE Areaname={0} UNION ALL SELECT a.YUnCode,a.PUnCode FROM dbo.Area a INNER JOIN CTE b ON b.YUnCode=a.PUnCode) SELECT a.*  FROM dbo.Area a INNER JOIN CTE t ON a.YUnCode=t.YUnCode";
                areaAllM = _rep.SqlQuery<Area>(sqlM, string.IsNullOrEmpty(city) ? "上海市" : city).Select(p => new Area { YunCode = p.YunCode }).ToList();
            }
            else
            {
                sqlM = "WITH CTE AS ( SELECT YUnCode,PUnCode FROM dbo.Area WHERE PUnCode={0} UNION ALL SELECT a.YUnCode,a.PUnCode FROM dbo.Area a INNER JOIN CTE b ON b.YUnCode=a.PUnCode) SELECT a.*  FROM dbo.Area a INNER JOIN CTE t ON a.YUnCode=t.YUnCode";
                areaAllM = _rep.SqlQuery<Area>(sqlM, province).Select(p => new Area { YunCode = p.YunCode }).ToList();
            }

            List<string> arrtM = new List<string>();
            foreach (var item in areaAllM)
                arrtM.Add(item.YunCode);
            string[] arrt2M = arrtM.ToArray();

            if (type == 0)
            {
                var MarketPriceTrendReport = _rep.Where<PlaceOfOriginPrice>(p => p.FishId == fishIdM && p.CreateTime >= Convert.ToDateTime(DateTime.Now.AddDays(-7))).ToList().OrderByDescending(p => p.CreateTime)
                    .GroupBy(p => new
                    {
                        Day = UtilsHelp.Condition(Convert.ToDateTime(p.CreateTime), "Day")
                    })
                    .Select(p => new
                    {
                        p.Key.Day,
                        Price = Math.Round(Convert.ToDecimal(p.Average(a => a.AveragePrice)), 2)
                    }).ToList();

                var MarketPriceTrendAreaReportAll = from a in _rep.Where<PlaceOfOriginPrice>(p => p.FishId == fishIdM && p.CreateTime >= Convert.ToDateTime(DateTime.Now.AddDays(-7)))
                                                    join m in _rep.Where<PlaceOfOriginUnit>(p => true) on a.PlaceOfOriginId equals m.Id
                                                    join r in _rep.Where<Area>(p => true) on m.AreaUnCode equals r.YunCode
                                                    where arrt2M.Contains(r.YunCode)
                                                    select new
                                                    {
                                                        a.CreateTime,
                                                        a.AveragePrice
                                                    };
                var MarketPriceTrendAreaReport = MarketPriceTrendAreaReportAll.ToList().OrderByDescending(p => p.CreateTime)
                                                   .GroupBy(p => new
                                                   {
                                                       Day = UtilsHelp.Condition(Convert.ToDateTime(p.CreateTime), "Day")
                                                   })
                                                  .Select(p => new
                                                  {
                                                      p.Key.Day,
                                                      Price = Math.Round(Convert.ToDecimal(p.Average(a => a.AveragePrice)), 2)
                                                  }).ToList().OrderByDescending(p => p.Day);


                return new ReturnValueModel { Code = 200, Msg = "success", Result = new { Date = new { National = MarketPriceTrendReport, Area = MarketPriceTrendReport } } };
            }

            if (type == 1)
            {
                var MarketPriceTrendReport = _rep.Where<PlaceOfOriginPrice>(p => p.FishId == fishIdM && p.CreateTime >= DateTime.Now.AddMonths(-3)).ToList().OrderByDescending(p => p.CreateTime)
                  .GroupBy(p => new
                  {
                      Month = UtilsHelp.Condition(Convert.ToDateTime(p.CreateTime), "Month")
                  })
                  .Select(p => new
                  {
                      Day = p.Key.Month,
                      Price = Math.Round(Convert.ToDecimal(p.Average(a => a.AveragePrice)), 2)
                  }).ToList();

                var MarketPriceTrendAreaReportAll = from a in _rep.Where<PlaceOfOriginPrice>(p => p.FishId == fishIdM && p.CreateTime >= Convert.ToDateTime(DateTime.Now.AddDays(-7)))
                                                    join m in _rep.Where<PlaceOfOriginUnit>(p => true) on a.PlaceOfOriginId equals m.Id
                                                    join r in _rep.Where<Area>(p => true) on m.AreaUnCode equals r.YunCode
                                                    where arrt2M.Contains(r.YunCode)
                                                    select new
                                                    {
                                                        a.CreateTime,
                                                        a.AveragePrice
                                                    };
                var MarketPriceTrendAreaReport = MarketPriceTrendAreaReportAll.ToList().OrderByDescending(p => p.CreateTime)
                                                   .GroupBy(p => new
                                                   {
                                                       Month = UtilsHelp.Condition(Convert.ToDateTime(p.CreateTime), "Month")
                                                   })
                                                  .Select(p => new
                                                  {
                                                      Day = p.Key.Month,
                                                      Price = Math.Round(Convert.ToDecimal(p.Average(a => a.AveragePrice)), 2)
                                                  }).ToList().OrderByDescending(p => p.Day);


                return new ReturnValueModel { Code = 200, Msg = "success", Result = new { Date = new { National = MarketPriceTrendReport, Area = MarketPriceTrendReport } } };
            }

            var MarketPriceTrendYReport = _rep.Where<PlaceOfOriginPrice>(p => p.FishId == fishIdM && p.CreateTime >= DateTime.Now.AddMonths(-12)).ToList().OrderByDescending(p => p.CreateTime)
                  .GroupBy(p => new
                  {
                      Month = UtilsHelp.Condition(Convert.ToDateTime(p.CreateTime), "Month")
                  })
                  .Select(p => new
                  {
                      Day = p.Key.Month,
                      Price = Math.Round(Convert.ToDecimal(p.Average(a => a.AveragePrice)), 2)
                  }).ToList();

            var MarketPriceTrendAreaYReportAll = from a in _rep.Where<PlaceOfOriginPrice>(p => p.FishId == fishIdM && p.CreateTime >= Convert.ToDateTime(DateTime.Now.AddDays(-7)))
                                                 join m in _rep.Where<PlaceOfOriginUnit>(p => true) on a.PlaceOfOriginId equals m.Id
                                                 join r in _rep.Where<Area>(p => true) on m.AreaUnCode equals r.YunCode
                                                 where arrt2M.Contains(r.YunCode)
                                                 select new
                                                 {
                                                     a.CreateTime,
                                                     a.AveragePrice
                                                 };
            var MarketPriceTrendAreaYReport = MarketPriceTrendAreaYReportAll.ToList().OrderByDescending(p => p.CreateTime)
                                               .GroupBy(p => new
                                               {
                                                   Month = UtilsHelp.Condition(Convert.ToDateTime(p.CreateTime), "Month")
                                               })
                                              .Select(p => new
                                              {
                                                  Day = p.Key.Month,
                                                  Price = Math.Round(Convert.ToDecimal(p.Average(a => a.AveragePrice)), 2)
                                              }).ToList().OrderByDescending(p => p.Day);

            return new ReturnValueModel { Code = 200, Msg = "success", Result = new { Date = new { National = MarketPriceTrendYReport, Area = MarketPriceTrendYReport } } };
        }


        /// <summary>
        /// 鱼种列表
        /// </summary>
        /// <param name="fishType">鱼种类别</param>
        /// <returns></returns>
        public ReturnValueModel GetFingerlingTypeAll(string fishType)
        {
            var fingerlingTypeList = _rep.Where<FishSpecies>(p => p.Status == 0)
                                        .Join(_rep.Where<Dictionary>(p => p.Name == fishType), a => a.Type, d => d.Id, (a, d) => new { Type = d.Name, a.FishName, a.FishCode }).DistinctBy(p => p.FishName);

            var models = fingerlingTypeList.ToList();

            return new ReturnValueModel { Code = 200, Msg = "success", Result = models };
        }

        public ReturnValueModel GetMarketPriceAll(int index, int size, string areaName, string fishType, string fishName)
        {
            var MarketPriceAll = from a in _rep.Where<MarketPrice>(p => p.Status == 0)
                                 where a.CreateTime >= DateTime.Now.AddMonths(-3) && a.CreateTime <= DateTime.Now
                                 join m in _rep.Where<MarketUnit>(p => p.Status == 0) on a.MarketId equals m.Id
                                 join r in _rep.Where<Area>(p => p.Status == 0) on m.AreaUnCode equals r.YunCode
                                 // join y in _rep.Where<YscUser>(p => p.Status == 0) on a.UserId equals y.Id
                                 join f in _rep.Where<FishSpecies>(p => p.Status == 0) on a.FishId equals f.FishCode
                                 join d in _rep.Where<Dictionary>(p => p.Status == 0) on f.Type equals d.Id
                                 select new
                                 {
                                     a.Id,
                                     f.FishName,
                                     fishType = d.Name,
                                     // y.UserName,
                                     a.UserId,
                                     m.Address,
                                     a.AveragePrice,
                                     a.CreateTime
                                 };


            var modelM = MarketPriceAll.OrderByDescending(p => p.CreateTime)
                    .WhereIf(p => p.FishName == fishName, string.IsNullOrEmpty(fishName) == false)
                    .WhereIf(p => p.fishType == fishType, string.IsNullOrEmpty(fishType) == false)
                    .ToPaginationList(index, size)
                    .ToList().Join(_rep.Where<YscUser>(p => p.Status == 0), a => a.UserId, y => y.Id, (a, y) => new { a.Id, a.FishName, y.UserName, a.Address, a.AveragePrice, a.CreateTime, a.UserId }).ToList();

            return new ReturnValueModel { Code = 200, Msg = "success", Result = modelM };
        }

        public ReturnValueModel GetPlaceOfOriginPriceAll(int index, int size, string areaName, string fishType, string fishName)
        {
            var PlaceOfOriginPriceAll = from a in _rep.Where<PlaceOfOriginPrice>(p => p.Status == 0)
                                        where a.CreateTime >= DateTime.Now.AddMonths(-3) && a.CreateTime <= DateTime.Now
                                        join m in _rep.Where<PlaceOfOriginUnit>(p => p.Status == 0) on a.PlaceOfOriginId equals m.Id
                                        join r in _rep.Where<Area>(p => p.Status == 0) on m.AreaUnCode equals r.YunCode
                                        // join y in _rep.Where<YscUser>(p => p.Status == 0) on a.UserId equals y.Id
                                        join f in _rep.Where<FishSpecies>(p => p.Status == 0) on a.FishId equals f.FishCode
                                        join d in _rep.Where<Dictionary>(p => p.Status == 0) on f.Type equals d.Id
                                        select new
                                        {
                                            a.Id,
                                            f.FishName,
                                            fishType = d.Name,
                                            // y.UserName,
                                            a.UserId,
                                            m.Address,
                                            a.AveragePrice,
                                            a.CreateTime
                                        };


            var model = PlaceOfOriginPriceAll.OrderByDescending(p => p.CreateTime)
                    .WhereIf(p => p.FishName == fishName, string.IsNullOrEmpty(fishName) == false)
                    .WhereIf(p => p.fishType == fishType, string.IsNullOrEmpty(fishType) == false)
                    .ToPaginationList(index, size)
                    .ToList().Join(_rep.Where<YscUser>(p => p.Status == 0), a => a.UserId, y => y.Id, (a, y) => new { a.Id, a.FishName, y.UserName, a.Address, a.AveragePrice, a.CreateTime, a.UserId }).ToList();

            return new ReturnValueModel { Code = 200, Msg = "success", Result = model };
        }

        /// <summary>
        /// 鱼种列表媒体
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="sasearch"></param>
        /// <returns></returns>
        public PaginationModel GetFishSpeciesAppAll(int index, int size, string sasearch)
        {
            var fishAll = from a in _rep.Where<FishSpecies>(p => p.Status == 0)
                          join m in _rep.Where<MediaImage>(p => p.Status == 0 && p.BllType == "鱼种图片") on a.FishName equals m.Type into m
                          from mi in m.DefaultIfEmpty()
                          select new
                          {
                              a.Id,
                              a.FishName,
                              a.FishCode,
                              a.EnglishName,
                              a.LatinName,
                              Url = mi.FileUrl,
                              a.SuName,
                              a.Introduction,
                              a.PengRen,
                              a.TiShi,
                              a.XuanGou,
                              a.YingYang,
                              a.Remarks,
                              a.CreateTime
                          };

            var fishList = fishAll.DistinctBy(p => p.FishName)
                .WhereIf(p => p.FishName.Contains(sasearch), string.IsNullOrEmpty(sasearch) == false);

            var models = fishList.ToPaginationList(index, size).ToList();

            return new PaginationModel { Code = 200, Msg = "success", Total = fishList.ToList().Count(), Result = new { Data = models } };
        }

        /// <summary>
        /// 地区树
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public object GetAreaTreeAll(string name)
        {
            if (!string.IsNullOrEmpty(name) && name != "undefined" && name != "null")
            {
                var allMenuList = _rep.Where<Area_copy>(p => p.Status == 0).OrderBy(p => p.YunCode).ToList();
                List<TreeModel> rootNodeList = new List<TreeModel>();
                foreach (var parentNodeList in allMenuList.Where(p => p.YunCode == Convert.ToInt32(name)))
                {
                    TreeModel menuNode = new TreeModel();
                    menuNode.id = parentNodeList.YunCode.ToString();
                    menuNode.name = parentNodeList.AreaName;
                    menuNode.open = "true";
                    menuNode.children = CreateChildTree(allMenuList, menuNode.id);
                    rootNodeList.Add(menuNode);
                }
                return rootNodeList;

            }
            else
            {
                var allMenuList = _rep.Where<Area_copy>(p => p.Status == 0).OrderBy(p => p.YunCode).ToList();
                List<TreeModel> rootNodeList = new List<TreeModel>();
                foreach (var parentNodeList in allMenuList.Where(t => t.PunCode == "0"))
                {
                    TreeModel menuNode = new TreeModel();
                    menuNode.id = parentNodeList.YunCode.ToString();
                    menuNode.name = parentNodeList.AreaName;
                    menuNode.open = "false";
                    menuNode.children = CreateChildTree(allMenuList, menuNode.id);
                    rootNodeList.Add(menuNode);
                }
                return rootNodeList;
            }




        }

        /// <summary>
        /// 递归生成子树
        /// </summary>
        /// <param name="AllMenuList"></param>
        /// <param name="vmMenu"></param>
        /// <returns></returns>
        private List<TreeModel> CreateChildTree(List<Area_copy> AllMenuList, string id)
        {
            // string parentMenuID = vmMenu.id;//根节点ID
            List<TreeModel> nodeList = new List<TreeModel>();
            var children = AllMenuList.Where(t => t.PunCode == id);
            foreach (var chl in children)
            {
                TreeModel node = new TreeModel();
                node.id = chl.YunCode.ToString();
                node.name = chl.AreaName;
                node.open = "true";
                node.children = CreateChildTree(AllMenuList, node.id);
                nodeList.Add(node);
            }
            return nodeList;
        }

        /// <summary>
        /// 鱼种树
        /// </summary>
        /// <returns></returns>
        public object GetFishTreeAll()
        {
            var allMenuList = _rep.Where<Dictionary>(p => p.Status == 0 && p.Type == "鱼种").ToList();
            List<TreeModel> rootNodeList = new List<TreeModel>();
            foreach (var parentNodeList in allMenuList)
            {
                TreeModel menuNode = new TreeModel();
                menuNode.id = parentNodeList.Name;
                menuNode.name = parentNodeList.Name;
                menuNode.open = "true";
                menuNode.children = CreateChildTree(parentNodeList.Id);
                rootNodeList.Add(menuNode);
            }
            return rootNodeList;
        }

        private List<TreeModel> CreateChildTree(string id)
        {
            // string parentMenuID = vmMenu.id;//根节点ID
            var children = _rep.Where<FishSpecies>(p => p.Status == 0 && p.Type == id).ToList();

            List<TreeModel> nodeList = new List<TreeModel>();
            foreach (var chl in children)
            {
                TreeModel node = new TreeModel();
                node.id = chl.FishName;
                node.name = chl.FishName;
                node.open = "true";
                //  node.children = CreateChildTree(AllMenuList, node.id);
                nodeList.Add(node);
            }
            return nodeList;
        }

        /// <summary>
        /// 市场鱼价统计分析
        /// </summary>
        /// <param name="province"></param>
        /// <param name="areName"></param>
        /// <param name="areCode"></param>
        /// <returns></returns>
        public PaginationModel GetMarkePriceStatiSticsReport(int province, string areName, string areCode)
        {
            if (province == 0)
                province = DateTime.Now.Year;

            var markePriceAll = _rep.Where<MarketPrice>(p => province != 0 ? p.CreateTime.Year == province : true);

            var markePriceTotal = markePriceAll.Count();
            //月
            if (province != 0)
            {
                var markePriceModethReport = markePriceAll.GroupBy(p => new { p.CreateTime.Month })
                   .Select(p => new
                   {
                       p.Key.Month,
                       Total = p.Count()
                   }).ToList(); 
                return new PaginationModel { Code = 200, Total = markePriceTotal, Msg = "success", Result = new { Data = markePriceModethReport } };
            }

            //年
            var markePriceYearReport = markePriceAll.GroupBy(p => new { p.CreateTime.Year })
                        .Select(p => new
                        {
                            p.Key.Year,
                            Total = p.Count()
                        }).ToList();
            return new PaginationModel { Code = 200, Total = markePriceTotal, Msg = "success", Result = new { Data = markePriceYearReport } };
        }

        public PaginationModel GetMarkePriceStatiSticsReport(string province, string areName, string areCode)
        {
            throw new NotImplementedException();
        }
    }
}
